<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="120px">
			<el-form-item label="移位任务编码" prop="archMoveNo">
				<el-input v-model="queryParams.archMoveNo" placeholder="请输入盒间移位任务编码" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="移位状态" prop="moveStatus">
				<el-select v-model="queryParams.moveStatus" placeholder="请选择移位状态" clearable>
					<el-option v-for="dict in shift_status" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>

			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<!--      <el-col :span="1.5">-->
			<!--        <el-button-->
			<!--          type="primary"-->
			<!--          plain-->
			<!--          icon="Plus"-->
			<!--          @click="handleAdd"-->
			<!--          v-hasPermi="['archive:ArchiveBoxinnerMoveTask:add']"-->
			<!--        >新增</el-button>-->
			<!--      </el-col>-->
			<!--      <el-col :span="1.5">-->
			<!--        <el-button-->
			<!--          type="success"-->
			<!--          plain-->
			<!--          icon="Edit"-->
			<!--          :disabled="single"-->
			<!--          @click="handleUpdate"-->
			<!--          v-hasPermi="['archive:ArchiveBoxinnerMoveTask:edit']"-->
			<!--        >修改</el-button>-->
			<!--      </el-col>-->
			<!--      <el-col :span="1.5">-->
			<!--        <el-button-->
			<!--          type="danger"-->
			<!--          plain-->
			<!--          icon="Delete"-->
			<!--          :disabled="multiple"-->
			<!--          @click="handleDelete"-->
			<!--          v-hasPermi="['archive:ArchiveBoxinnerMoveTask:remove']"-->
			<!--        >删除</el-button>-->
			<!--      </el-col>-->
			<!-- <el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['archive:ArchiveBoxinnerMoveTask:export']">导出</el-button>
			</el-col> -->
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="ArchiveBoxinnerMoveTaskList" @selection-change="handleSelectionChange">
			<!-- <el-table-column type="selection" width="55" align="center" /> -->
			<el-table-column label="序号" align='center' width="100">
				<template #default="scope">
					{{ scope.$index+1 }}
				</template>
			</el-table-column>
			<el-table-column label="移位任务编码" align="center" prop="archMoveNo" />
			<el-table-column label="移位状态" align="center" prop="moveStatus">
				<template #default="scope">
					<dict-tag :options="shift_status" :value="scope.row.moveStatus" />
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" fixed="right" width="300">
				<template #default="scope">
					<el-button type="text" icon="View" @click="handleView(scope.row, scope.index)" v-hasPermi="['archive:ArchiveBoxinnerMoveTask:query']">详细</el-button>
					&nbsp;
					<el-button v-if="scope.row.moveStatus == '01'" type="text" icon="Upload" @click="submitStatus(scope.row, scope.index)"
					 v-hasPermi="['archive:ArchiveBoxMoveTask:edit']">移位完成</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
		 @pagination="getList" />
		<!-- 查看详情 -->
		<el-dialog title="盒间档案移位详情" v-model="openscan" width="90%" append-to-body>
			<el-form ref="ArchiveBoxinnerMoveTaskRef" :model="form" label-width="170px">
				<el-row>
					<el-col :span="6">
						<el-form-item label="移位任务编码" prop="archMoveNo">
							<el-input v-model="form.archMoveNo" style="width:230px;" disabled="disabled" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="移位状态" prop="moveStatus">
							<el-select v-model="form.moveStatus" style="width:230px;" disabled="disabled">
								<el-option v-for="dict in shift_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				<el-divider content-position="center">盒间档案移位详情</el-divider>
				<!-- <el-row :gutter="10" class="mb8">
					<template #default="scope">
						<el-col :span="1.5">
							<el-button type="primary" icon="CircleCheck" @click="handleSave()" v-hasPermi="['archive:ArchiveBoxMoveTask:edit']">保存变更</el-button>
						</el-col>
						<el-col :span="3">
							<el-input v-model="inputhh" placeholder="请输入新档案盒号,统一设置" style="width:200px;" maxlength="20" @input="HHChange"
							 @focus="HHFocus"></el-input>
						</el-col>
						<el-col :span="3">
							<el-input v-model="inputdh" placeholder="请输入新档案袋号,统一设置" style="width:200px;" maxlength="20" @input="DHChange"
							 @focus="DHFocus"></el-input>
						</el-col>
						<el-col :span="3">
							<el-input v-model="inputkwh" placeholder="请输入新库位号,统一设置" style="width:200px;" maxlength="20" @input="KWChange"
							 @focus="KWFocus"></el-input>
						</el-col>
					</template>
				</el-row> -->
				<el-row>
					<el-table :data="archiveMoveList" ref="archiveBoxinnerMove" style="border:1px solid #dcdfe6;min-height: 550px;max-height: 550px;"
					 height="550" border>
						<el-table-column label="序号" align='center' width="50">
							<template #default="scope">
								{{ scope.$index+1 }}
							</template>
						</el-table-column>
						<el-table-column label="新档案盒号" prop="newBoxNo" align="center" width="150">
							<!-- <template #default="scope">
								<el-input v-model="scope.row.newBoxNo" maxlength="10"></el-input>
							</template> -->
						</el-table-column>
						<el-table-column label="新档案袋号" prop="newBagNo" align="center" width="150">
							<!-- <template #default="scope">
								<el-input v-model="scope.row.newBagNo" maxlength="10"></el-input>
							</template> -->
						</el-table-column>
						<el-table-column label="原盒盒满标志" prop="oldBoxFullStatus" align="center" width="150">
							<template #default="scope">
									<dict-tag :options="box_full_flag" :value="scope.row.oldBoxFullStatus" />
								</template>
							</el-table-column>
						<el-table-column label="新盒盒满标志" prop="newBoxFullStatus" align="center" width="150">
						<template #default="scope">
								<dict-tag :options="box_full_flag" :value="scope.row.newBoxFullStatus" />
							</template>
						</el-table-column>
						<el-table-column label="档案区编号" width="150" align="center" prop="areaNo" />
						<el-table-column label="档案柜编号" align="center" prop="cabinetNo" width="150" />
						<el-table-column label="档案盒号" align="center" prop="boxNo" width="150" />
						<el-table-column label="档案盒位置" align="center" prop="storLoc" width="300" />
						<el-table-column label="资料名称" align="center" prop="archiveName" width="330" />
						<el-table-column label="档案类型" align="center" prop="archiveType" width="150">
							<template #default="scope">
								<dict-tag :options="archive_type" :value="scope.row.archiveType" />
							</template>
						</el-table-column>
						<el-table-column label="档案密级" align="center" prop="conLevel" width="100">
							<template #default="scope">
								<dict-tag :options="confd_lv" :value="scope.row.conLevel" />
							</template>
						</el-table-column>
						<el-table-column label="保管期限" align="center" prop="saveDate" width="100">
							<template #default="scope">
								<dict-tag :options="keep_dur" :value="scope.row.saveDate" />
							</template>
						</el-table-column>
						<el-table-column label="资料种类	" align="center" prop="meansType" width="150">
							<template #default="scope">
								<dict-tag :options="means_type" :value="scope.row.meansType" />
							</template>
						</el-table-column>
						<el-table-column label="介质类型" align="center" prop="medForm">
							<template #default="scope">
								<dict-tag :options="med_form" :value="scope.row.medForm" />
							</template>
						</el-table-column>
						<el-table-column label="对象类型" align="center" prop="archObjType" width="150">
							<template #default="scope">
								<dict-tag :options="arch_obj_type" :value="scope.row.archObjType" />
							</template>
						</el-table-column>
						<el-table-column label="对象编号" align="center" prop="archObjNo" width="150" />
						<el-table-column label="对象名称" align="center" prop="archObjName" />
					</el-table>
				</el-row>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button @click="openscan = false">关 闭</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="ArchiveBoxinnerMoveTask">
	import {
		listArchiveBoxinnerMoveTask,
		getArchiveBoxinnerMoveTask,
		delArchiveBoxinnerMoveTask,
		addArchiveBoxinnerMoveTask,
		updateArchiveBoxinnerMoveTask,
		selectArchiveBoxinnerMoveByTaskId,
		editarchiveBoxinnerMovekList,
		updateArchiveBoxinnerMoveTaskStatus,
		archiveBoxinnerMoveTaskResultReport
	} from "@/api/archive/ArchiveBoxinnerMoveTask";
	import {
		getCurrentInstance,
		reactive,
		ref,
		toRefs
	} from "vue";
	const {
		proxy
	} = getCurrentInstance();

	const ArchiveBoxinnerMoveTaskList = ref([]);
	const open = ref(false);
	const loading = ref(true);
	const showSearch = ref(true);
	const ids = ref([]);
	const single = ref(true);
	const multiple = ref(true);
	const total = ref(0);
	const title = ref("");
	const dateRange = ref([]);
	const openscan = ref(false);
	const archiveMoveList = ref([]);
	const data = reactive({
		form: {},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			archMoveNo: null,
			orgNo: null,
			roomNo: null,
			pushStatus: null,
			pushTime: null,
			status: null,
			reportTime: null
		},
		rules: {}
	});
	let inputkwh = ref('');
	let inputdh = ref('');
	let inputhh = ref('');
	const {
		queryParams,
		form,
		rules
	} = toRefs(data);
	const {
		box_full_flag,
		push_status,
		execut_result,
		shift_status,
		archive_type,
		save_extended,
		borrow_flag,
		keep_dur,
		confd_lv,
		med_form,
		means_type,
		arch_obj_type
	} = proxy.useDict('box_full_flag','push_status', 'execut_result', 'shift_status', 'archive_type', 'save_extended', 'borrow_flag',
		'keep_dur', 'confd_lv', 'med_form', 'means_type', 'arch_obj_type');

	/** 查询盒间移位任务列表 */
	function getList() {
		loading.value = true;
		listArchiveBoxinnerMoveTask(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
			// listArchiveBoxinnerMoveTask(queryParams.value).then(response => {
			ArchiveBoxinnerMoveTaskList.value = response.rows;
			total.value = response.total;
			loading.value = false;
		});
	}

	// 取消按钮
	function cancel() {
		open.value = false;
		reset();
	}

	// 表单重置
	function reset() {
		form.value = {
			taskId: null,
			archMoveNo: null,
			orgNo: null,
			roomNo: null,
			pushStatus: "0",
			pushTime: null,
			status: "0",
			reportTime: null
		};
		proxy.resetForm("ArchiveBoxinnerMoveTaskRef");
		archiveMoveList.value = [];
		inputkwh.value = '';
		inputdh.value = '';
		inputhh.value = '';
	}

	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	}

	/** 重置按钮操作 */
	function resetQuery() {
		dateRange.value = [];
		proxy.resetForm("queryRef");
		handleQuery();
	}

	// 多选框选中数据
	function handleSelectionChange(selection) {
		ids.value = selection.map(item => item.taskId);
		single.value = selection.length != 1;
		multiple.value = !selection.length;
	}

	/** 新增按钮操作 */
	function handleAdd() {
		reset();
		open.value = true;
		title.value = "添加盒间移位任务";
	}

	/** 修改按钮操作 */
	function handleUpdate(row) {
		reset();
		const taskId = row.taskId || ids.value
		getArchiveBoxinnerMoveTask(taskId).then(response => {
			form.value = response.data;
			open.value = true;
			title.value = "修改盒间移位任务";
		});
	}

	/** 提交按钮 */
	function submitForm() {
		proxy.$refs["ArchiveBoxinnerMoveTaskRef"].validate(valid => {
			if (valid) {
				if (form.value.taskId != null) {
					updateArchiveBoxinnerMoveTask(form.value).then(response => {
						proxy.$modal.msgSuccess("修改成功");
						open.value = false;
						getList();
					});
				} else {
					addArchiveBoxinnerMoveTask(form.value).then(response => {
						proxy.$modal.msgSuccess("新增成功");
						open.value = false;
						getList();
					});
				}
			}
		});
	}

	/** 删除按钮操作 */
	function handleDelete(row) {
		const taskIds = row.taskId || ids.value;
		proxy.$modal.confirm('是否确认删除盒间移位任务编号为"' + taskIds + '"的数据项？').then(function() {
			return delArchiveBoxinnerMoveTask(taskIds);
		}).then(() => {
			getList();
			proxy.$modal.msgSuccess("删除成功");
		}).catch(() => {});
	}

	/** 导出按钮操作 */
	function handleExport() {
		proxy.download('archive/ArchiveBoxinnerMoveTask/export', {
			...queryParams.value
		}, `盒间档案移位_${new Date().getTime()}.xlsx`)
	}

	function handleView(row) {
		openscan.value = true;
		inputkwh.value = '';
		inputdh.value = '';
		inputhh.value = '';
		const taskId = row.taskId;
		getArchiveBoxinnerMoveTask(taskId).then(response => {
			form.value = response.data;
		});
		selectArchiveBoxinnerMoveByTaskId(taskId).then(response => {
			archiveMoveList.value = response.data;
		});
	}

	function submitReport(row) {
		const archMoveNo = row.archMoveNo;

		if (row.moveStatus == "01") {
			proxy.$modal.msgError('移位状态为【待移位】，上报失败！');
		} else if (row.moveStatus === "02") {
			form.value.archMoveNo = archMoveNo;
			archiveBoxinnerMoveTaskResultReport(form.value).then(response => {
				proxy.$modal.msgSuccess("移位信息上报成功");
				getList();
			}).catch(error => {
				console.error("移位信息上报失败:", error);
			});
		}
		// }

	}

	function handleSave(row) {
		// editarchiveBoxMoveList(archiveMoveList.value).then(response => {
		editarchiveBoxinnerMovekList(archiveMoveList.value).then(response => {
			// {"msg":"操作成功","code":200}
			if (response.code == "200") {
				proxy.$modal.msgSuccess('保存成功！');
				getList();
				openscan.value = false;
			} else {
				proxy.$modal.msgError(response.msg);
			}
		});
	}

	function KWChange(e) {
		inputkwh.value = e;
		archiveMoveList.value.forEach((item, index) => {
			item.newHouseLocationNo = inputkwh.value;
		});
	}

	function KWFocus(e) {
		archiveMoveList.value.forEach((item, index) => {
			item.newHouseLocationNo = '';
		});
	}

	function DHChange(e) {
		inputdh.value = e;
		archiveMoveList.value.forEach((item, index) => {
			item.newBagNo = inputdh.value;
		});
	}

	function DHFocus(e) {
		archiveMoveList.value.forEach((item, index) => {
			item.newBagNo = '';
		});
	}

	function HHChange(e) {
		inputhh.value = e;
		archiveMoveList.value.forEach((item, index) => {
			item.newBoxNo = inputhh.value;
		});
	}

	function HHFocus(e) {
		archiveMoveList.value.forEach((item, index) => {
			item.newBoxNo = '';
		});
	}


	function submitStatus(row) {
		const taskId = row.taskId;
		if (row.boxMoveStatus == "02") {
			proxy.$modal.msgError('单据状态为【移位完成】，不需要重复点击！');
		} else {
			getArchiveBoxinnerMoveTask(taskId).then(response => {
				updateArchiveBoxinnerMoveTaskStatus(response.data).then(response => {
					// {"msg":"操作成功","code":200}
					if (response.code == "500") {
						proxy.$modal.msgError(response.msg);
					} else if (response.code = "200") {
						proxy.$modal.msgSuccess("移位完成！");
						getList();
					}
				});
			});
		}
	}
	getList();
</script>
